/*
 * ButtonsDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import com.cute4j.swing.Button;
import com.cute4j.swing.CheckBox;
import com.cute4j.swing.ColorChooser;
import com.cute4j.swing.Component;
import com.cute4j.swing.Container;
import com.cute4j.swing.FontChooser;
import com.cute4j.swing.Icon;
import com.cute4j.swing.ImageIcon;
import com.cute4j.swing.Menu;
import com.cute4j.swing.MenuItem;
import com.cute4j.swing.RadioButton;
import com.cute4j.swing.ScrollPane;
import com.cute4j.swing.SelectableGroup;
import com.cute4j.swing.Separator;
import com.cute4j.swing.ToggleButton;
import com.cute4j.swing.ToolButton;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.graphics.Color;
import com.cute4j.swing.graphics.Font;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;
import com.cute4j.swingx.actions.AboutQtAction;
import com.cute4j.swingx.actions.AboutCute4jAction;

/**
 * Demonstration of the Button, ToggleButon, ToolButon, CheckBox and
 * RadioButton classes.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class ButtonsDemo extends DemoModule {

// Private Fields. -------------------------------------------------------------

  private Component coloredPushButton;
  private Component coloredToggleButton;
  private Component coloredToolButton;
  private Component coloredCheckButton;
  private Component coloredRadioButton;

// Public Constructor. ---------------------------------------------------------

  /**
   * Constructs new ButtonsDemo object.
   */
  public ButtonsDemo() {
    super("Buttons Demo", "Demonstration of the Buton, ToggleButon, " +
                          "ToolButon, CheckBox and RadioButton classes.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    FormLayout layout = new FormLayout(
                          "p, 4dlu, p, 4dlu, p, 4dlu, p, r:24px," +
                          "c:p, l:24px, 18dlu, p, 4dlu, p, 4dlu, p",
                          "p, 2dlu, p, 4dlu, p, 2dlu, p, 4dlu, p," +
                          " 2dlu, p, 4dlu, p, 2dlu, p, 4dlu, p, 2dlu, p, 4dlu");
    Container panel = new Container(layout);
    panel.setInsets(16, 16, 16, 16);
    CellConstraints cc = new CellConstraints();
    buildButtons(panel, cc);
    buildToggleButtons(panel, cc);
    buildToolButtons(panel, cc);
    buildCheckBoxs(panel, cc);
    buildRadioButtons(panel, cc);
    buildSettings(panel, cc);
    setColor(Color.RED);
    return new ScrollPane(panel);
  }

// Private Methods. ------------------------------------------------------------

  private void buildButtons(Container panel, CellConstraints cc) {
    panel.add(new Separator("Buttons"), cc.xyw(1, 1, 10));
    panel.add(new Button("Enabled"), cc.xy(1, 3));
    panel.add(new Button("Disabled"), cc.xy(3, 3)).setEnabled(false);
    Button button = new Button("Default");
    button.setDefaultCapable(true);
    panel.add(button, cc.xy(5, 3));
    coloredPushButton = panel.add(new Button("ColoRED"), cc.xy(7, 3));
    button = new Button(createIcon("Button.png"));
    button.setRolloverIcon(createIcon("ButtonRollover.png"));
    button.setPressedIcon(createIcon("ButtonPressed.png"));
    panel.add(button, cc.xy(9, 3));
  }

  private void buildToggleButtons(Container panel, CellConstraints cc) {
    panel.add(new Separator("ToggleButtons"), cc.xyw(1, 5, 10));
    panel.add(new ToggleButton("Enabled"), cc.xy(1, 7));
    panel.add(new ToggleButton("Disabled"), cc.xy(3, 7)).setEnabled(false);
    ToggleButton toggleButton = new ToggleButton("Selected");
    toggleButton.setSelected(true);
    panel.add(toggleButton, cc.xy(5, 7));
    coloredToggleButton = panel.add(new ToggleButton("ColoRED"), cc.xy(7, 7));
    toggleButton = new ToggleButton(createIcon("ToggleButton.png"));
    toggleButton.setPressedIcon(createIcon("ToggleButtonSelected.png"));
    toggleButton.setSelectedIcon(createIcon("ToggleButtonSelected.png"));
    panel.add(toggleButton, cc.xy(9, 7));
  }

  private void buildToolButtons(Container panel, CellConstraints cc) {
    panel.add(new Separator("ToolButtons"), cc.xyw(1, 9, 10));
    panel.add(new ToolButton("Enabled"), cc.xy(1, 11));
    panel.add(new ToolButton("Disabled"), cc.xy(3, 11)).setEnabled(false);
    ToolButton toolButton = new ToolButton("Menu");
    Menu menu = new Menu();
    MenuItem menuItem = menu.add(new MenuItem(new AboutCute4jAction()));
    menuItem.setIcon(createIcon("GreenTeaCup.png"));
    menuItem.setRolloverIcon(createIcon("GreenTeaCupRollover.png"));
    menu.addSeparator();
    menuItem = menu.add(new MenuItem(new AboutQtAction()));
    menuItem.setIcon(createIcon("QtLogo.png"));
    toolButton.setMenu(menu);
    panel.add(toolButton, cc.xy(5, 11));
    coloredToolButton = panel.add(new ToolButton("ColoRED"), cc.xy(7, 11));
    toolButton = new ToolButton(createIcon("ToolButton.png"));
    toolButton.setRolloverIcon(createIcon("ToolButtonRollover.png"));
    toolButton.setPressedIcon(createIcon("ToolButtonPressed.png"));
    panel.add(toolButton, cc.xy(9, 11));
  }

  private void buildCheckBoxs(Container panel, CellConstraints cc) {
    panel.add(new Separator("CheckBox"), cc.xyw(1, 13, 10));
    panel.add(new CheckBox("Enabled"), cc.xy(1, 15));
    panel.add(new CheckBox("Disabled"), cc.xy(3, 15)).setEnabled(false);
    CheckBox checkBox = new CheckBox("Selected");
    checkBox.setSelected(true);
    panel.add(checkBox, cc.xy(5, 15));
    coloredCheckButton = panel.add(new CheckBox("ColoRED"), cc.xy(7, 15));
    checkBox = new CheckBox(createIcon("CheckBox.png"));
    checkBox.setSelectedIcon(createIcon("CheckBoxSelected.png"));
    checkBox.setRolloverIcon(createIcon("CheckBoxRollover.png"));
    checkBox.setRolloverSelectedIcon(createIcon(
                                              "CheckBoxRolloverSelected.png"));
    panel.add(checkBox, cc.xy(9, 15));
  }

  private void buildRadioButtons(Container panel, CellConstraints cc) {
    panel.add(new Separator("RadioButton"), cc.xyw(1, 17, 10));
    panel.add(new RadioButton("Enabled"), cc.xy(1, 19));
    panel.add(new RadioButton("Disabled"), cc.xy(3, 19)).setEnabled(false);
    RadioButton radioButton = new RadioButton("Selected");
    radioButton.setSelected(true);
    panel.add(radioButton, cc.xy(5, 19));
    coloredRadioButton = panel.add(new RadioButton("ColoRED"), cc.xy(7, 19));
    Icon icon = createIcon("RadioButton.png");
    Icon iconPressed = createIcon("RadioButtonPressed.png");
    Icon iconSelected = createIcon("RadioButtonSelected.png");
    Icon iconRollover = createIcon("RadioButtonRollover.png");
    Icon iconRolloverSelected = createIcon("RadioButtonRolloverSelected.png");
    SelectableGroup sg = new SelectableGroup();
    radioButton = new RadioButton(icon);
    radioButton.setPressedIcon(iconPressed);
    radioButton.setSelectedIcon(iconSelected);
    radioButton.setRolloverIcon(iconRollover);
    radioButton.setRolloverSelectedIcon(iconRolloverSelected);
    panel.add(radioButton, cc.xy(8, 19));
    sg.add(radioButton);
    radioButton = new RadioButton(icon);
    radioButton.setPressedIcon(iconPressed);
    radioButton.setSelectedIcon(iconSelected);
    radioButton.setRolloverIcon(iconRollover);
    radioButton.setRolloverSelectedIcon(iconRolloverSelected);
    radioButton.setSelected(true);
    panel.add(radioButton, cc.xy(9, 19));
    sg.add(radioButton);
    radioButton = new RadioButton(icon);
    radioButton.setPressedIcon(iconPressed);
    radioButton.setSelectedIcon(iconSelected);
    radioButton.setRolloverIcon(iconRollover);
    radioButton.setRolloverSelectedIcon(iconRolloverSelected);
    panel.add(radioButton, cc.xy(10, 19));
    sg.add(radioButton);
  }

  private void buildSettings(final Container panel, CellConstraints cc) {
    panel.add(new Separator(Separator.VERTICAL), cc.xyh(12, 1, 20));
    Button colorSettings = new Button("Color");
    colorSettings.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        setColor(ColorChooser.getColor(panel,
                                       coloredPushButton.getForeground()));
      }
    });
    panel.add(colorSettings, cc.xy(14, 3, "c, t"));
    Button fontSettings = new Button("Font");
    fontSettings.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        setFont(FontChooser.getFont(panel, coloredPushButton.getFont()));
      }
    });
    panel.add(fontSettings, cc.xy(16, 3, "c, t"));
  }

  private void setColor(Color color) {
    if (color != null) {
      coloredPushButton.setForeground(color);
      coloredToggleButton.setForeground(color);
      coloredToolButton.setForeground(color);
      coloredCheckButton.setForeground(color);
      coloredRadioButton.setForeground(color);
    }
  }

  private void setFont(Font font) {
    if (font != null) {
      coloredPushButton.setFont(font);
      coloredToggleButton.setFont(font);
      coloredToolButton.setFont(font);
      coloredCheckButton.setFont(font);
      coloredRadioButton.setFont(font);
    }
  }

  private ImageIcon createIcon(String iconName) {
    return new ImageIcon(loadResource("resources/images/buttons/" + iconName));
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new ButtonsDemo().show();
  }

}
